package com.lich.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.lich.entity.Product;
import com.lich.entity.Users;
import com.lich.entity.vo.Result;
import com.lich.service.ProductService;

/**
 * @description 页面控制器
 * @date 2016年8月17日
 * @author lich-thinkpad
 */
@Controller
public class PageController {
	private static Logger logger = LoggerFactory.getLogger(PageController.class);
	@Resource
	ProductService productService;

	/**
	 * @description 跳转登陆页面
	 * @date 2016年8月29日
	 * @author lich-thinkpad
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String loginPage() {
		return "login";
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String registerPage() {
		return "register";
	}

	/**
	 * @description 后台管理主页面
	 * @date 2016年8月29日
	 * @author lich-thinkpad
	 * @return
	 */
	@RequestMapping(value = "/manager/main", method = RequestMethod.GET)
	public ModelAndView mainPage(HttpServletRequest request) {
		Result<Object> result = new Result<>();
		HttpSession session = request.getSession();
		Users currentUser = (Users) session.getAttribute("currentUser");
		if (currentUser == null) {
			result.setSuccess(false);
			result.setMessage("获取当前用户信息失败");
			logger.error("获取当前用户信息失败");
		} else if (Users.MANAGER.equals(currentUser.getRole())) {
			List<Product> products = productService.getByUserId(currentUser.getId());
			result.setObject(products);
			result.setSuccess(true);
			result.setMessage("获取产品数据成功");
		} else if (Users.ADMIN.equals(currentUser.getRole())) {
			List<Product> products = productService.getAll();
			result.setObject(products);
			result.setSuccess(true);
			result.setMessage("获取产品数据成功");
		}
		return new ModelAndView("manager/main", "result", result);
	}

	/**
	 * @description 登录时间图表
	 * @date 2016年8月30日
	 * @author lich-thinkpad
	 * @return
	 */
	@RequestMapping(value = "/manager/liveness/loginTime", method = RequestMethod.GET)
	public String loginTimeChart() {
		return "manager/liveness/loginTime";
	}

	/**
	 * @description 登录次数图表
	 * @date 2016年8月30日
	 * @author lich-thinkpad
	 * @return
	 */
	@RequestMapping(value = "/manager/liveness/loginTimes", method = RequestMethod.POST)
	public ModelAndView loginTimesChart(String productId) {
		//System.out.println(productId);
				
		return new ModelAndView("manager/liveness/loginTimes", "productId", productId);
	}
	/**
	 * @description 活跃用户图表
	 * @date 2016年9月13日
	 * @author lich-thinkpad
	 * @return
	 */
	@RequestMapping(value = "/manager/liveness/clientLiveness", method = RequestMethod.POST)
	public ModelAndView clientLivenessChart(String productId) {
		//System.out.println(productId);
				
		return new ModelAndView("manager/liveness/clientLiveness", "productId", productId);
	}

	/**
	 * @description 跳转403页面
	 * @date 2016年8月29日
	 * @author lich-thinkpad
	 * @return
	 */
	@RequestMapping(value = "/403", method = RequestMethod.GET)
	public String _403Page() {
		return "403";
	}
	@RequestMapping(value = "/404", method = RequestMethod.GET)
	public String _404Page() {
		return "404";
	}
	@RequestMapping(value = "/405", method = RequestMethod.GET)
	public String _405Page() {
		return "405";
	}
	@RequestMapping(value = "/500", method = RequestMethod.GET)
	public String _500Page() {
		return "500";
	}
}
